package cn.tedu.api;

import org.junit.jupiter.api.Test;

import java.util.Objects;

/*本类用与测试顶级父类Object*/
public class TestObject {
    @Test
    public void test(){
        Student s = new Student("效小", 123);
        Student s1 = new Student("效小", 123);
        System.out.println(s);
        //测试hashCode

        System.out.println(s.hashCode());//1728790703
        System.out.println(s1.hashCode());//1227074340
        /*equals方法的默认实现是==比较，比较的是两个对象的地址值，==比较的是栈内的值
        * 如果不想比较地址值，想比较类型+所有属性值，需要重写此方法*/
        System.out.println(s.equals(s1));
    }
}
class Student{
    String name;
    int age;

    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

  /*  @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }*/

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
